// Copyright © 2017 Giovanni Squillero <giovanni.squillero@polito.it>
// https://github.com/squillero/computer-sciences
// Free under certain conditions — see the license for details.

#include <stdio.h>
#include <stdlib.h>

#define NUM_BITS 16

int main()
{
    printf("bin2sm (%d bits)!\n", NUM_BITS);

    int dec;
    int bin_digits[NUM_BITS] = { 0 };

    printf("Dec: ");
    scanf("%d", &dec);

    if(dec < 0) {
        bin_digits[0] = 1;
        dec = -dec;
    }

    int pos = NUM_BITS - 1;
    while(dec != 0) {
        bin_digits[pos--] = dec % 2;
        dec /= 2;
    }

    printf("S&M: ");
    for(int t = 0; t < NUM_BITS; ++t) {
        printf("%d", bin_digits[t]);
    }
    printf("\n");

    printf("1sC: ");
    printf("%d", bin_digits[0]);
    for(int t = 1; t < NUM_BITS; ++t) {
        if(bin_digits[0] == 0) {
            printf("%d", bin_digits[t]);
        } else {
            printf("%d", 1 - bin_digits[t]);
        }
    }
    printf("\n");

    return 0;
}
